package com.quyunshuo.module.home.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.core.content.ContextCompat;

import com.quyunshuo.module.home.R;


public class MyCleanEditText extends AppCompatEditText implements View.OnFocusChangeListener, TextWatcher {
    private static final String TAG = "MyCleanEditText";
    private Drawable compoundDrawable;
    private Drawable drawable;

    public MyCleanEditText(@NonNull Context context) {
        this(context,null);
    }

    public MyCleanEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,R.attr.editTextStyle);
    }

    public MyCleanEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint(context);
    }

    private void initPaint(Context context) {
        Log.d(TAG, "initPaint: ");
        compoundDrawable = getCompoundDrawables()[2];
        if (compoundDrawable ==null) {
            compoundDrawable =  ContextCompat.getDrawable(context, R.drawable.ic_delete_gray);
        }
        //当通过代码动态设置 Drawable 时，系统​​不会自动计算其尺寸和位置​​，必须手动指定。
        //不设置，显示不出来。
        compoundDrawable.setBounds(0,0,compoundDrawable.getIntrinsicWidth(),compoundDrawable.getIntrinsicHeight());
        setCleanIcon(false);
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
    }

    private void setCleanIcon(boolean flag) {
        drawable = null;
        if (flag){
            drawable = compoundDrawable;
        }
        Log.d(TAG, "setCleanIcon: "+ drawable);
        setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1], drawable,getCompoundDrawables()[3]);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        Log.d(TAG, "onFocusChange: "+hasFocus);
        if (hasFocus && !TextUtils.isEmpty(getText().toString())) {
            setCleanIcon(true);
        }else{
            setCleanIcon(false);

        }

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.d(TAG, "onTextChanged: "+TextUtils.isEmpty(s));
        if (TextUtils.isEmpty(s)) {
            setCleanIcon(false);
        }else{
            setCleanIcon(true);

        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP :
                if (drawable!=null) {
                    if (
                            /*
                            https://www.jianshu.com/p/6b2b0f6873db
                            getTotalPaddingRight()图标左边缘至控件右边缘的距离，也就是D->E的距离
                            为什么没有y
                                无论用户点击右侧区域的哪个垂直位置，只要 X 坐标在范围内，都会触发清空文本操作。

                             */
                            event.getX()>getWidth()- getTotalPaddingRight() &&
                                    event.getX()< getWidth()-getPaddingRight()
                    ) {
                        //609,0,96,0
                        Log.d(TAG, "onTouchEvent: "+getWidth()+","+getPaddingRight()+","+getTotalPaddingRight()+","+getCompoundDrawablePadding());

                        setText("");
                    }
                }
                break;
        }
        return super.onTouchEvent(event);
    }
}
